package com.nf.health.app.inquiry.impl.viewholder;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.inquiry.bean.MySeltctPayTime;
import com.nf.health.app.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 订购日期 ，选择去订购
 * @author  于亚豪、
 *
 */
public class DoctorOrderPayHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    @Bind(R.id.gv_order_date)
    GridView gv_order_date;

    @Bind(R.id.tv_doctor_goodness_doc_price)
    TextView tv_doctor_goodness_doc_price;//订购所附方式总格
    private LayoutInflater layoutinflater;


    private Activity cnt;
    public DoctorOrderPayHolder(View itemView, Activity cnt,OnMyClickLintener onMyClickLintener) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.cnt = cnt;
       this. onMyClickLintener =onMyClickLintener;
        layoutinflater = LayoutInflater.from(cnt);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.ll_shouqi_icon_doc:
            case  R.id.tv_query_doc_detail:
                break;
        }
    }

    public void loadData(List<MySeltctPayTime> list){
       /* if(doctorDetail == null){
            return ;
        }*/
        final List<MySeltctPayTime> list2 = new ArrayList<MySeltctPayTime>();
        list2.clear();
        if(gv_order_date.getChildCount() > 0){
            gv_order_date.removeAllViews();
        }
        for (int i = 0; i < 5; i++) {
        MySeltctPayTime mySelectpayTime = new MySeltctPayTime();
            switch (i){
                case 0:  mySelectpayTime.setSelestDate("周");
                    break;
                case 1:    mySelectpayTime.setSelestDate("月");
                    break;
                case 2: mySelectpayTime.setSelestDate("季度");
                    break;
                case 3: mySelectpayTime.setSelestDate("半年");
                    break;
                case 4:mySelectpayTime.setSelestDate("一年");
                    break;
            }
            list2.add(mySelectpayTime);
        }
        tv_doctor_goodness_doc_price.setText(TextUtils.isEmpty("") ? "金额：" + Html.fromHtml("<font color='#FF9F1A'>"
                +  "¥"+ 0+"</font>") : Html.fromHtml("<font color='#FF9F1A'>"
                + "¥" + 85 + "</font>"));
        final MyAdapter myAdapter = new MyAdapter(cnt,list2);
        gv_order_date.setAdapter(myAdapter);
        gv_order_date.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MySeltctPayTime mySelectpayTime = list2.get(position);
                mySelectpayTime.setIsClick(true);
                dealNotSelectStatus(list2, position, myAdapter);
                if(onMyClickLintener != null){
                    onMyClickLintener.goToPayMoneyOrderService(mySelectpayTime.getSelestDate());
                }
            }
        });
    }
    private void dealNotSelectStatus(List<MySeltctPayTime> list2,int index,MyAdapter myAdapter){
        for (int i = 0; i < list2.size(); i++) {
            if(i != index){
                list2.get(i).setIsClick(false);
            }
        }
         myAdapter.setMyNotifyDataChanged(list2);
    }
    public interface   OnMyClickLintener{
            void goToPayMoneyOrderService(Object obj);
    }
    public OnMyClickLintener onMyClickLintener;
   /* public void regisitGoToPayMoneyOrderService(OnMyClickLintener onMyClickLintener){
        this.onMyClickLintener =onMyClickLintener;
    }*/





    private class MyAdapter extends BaseAdapter {
        private List<MySeltctPayTime> list;
        private Context cnt;

        private MyAdapter (Context cnt,List<MySeltctPayTime> list){
            this.cnt = cnt;
            this.list = list;
        }
        private void setMyNotifyDataChanged (List<MySeltctPayTime> list){
            this.list = list;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return  list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if(convertView == null){
                convertView = layoutinflater.inflate(R.layout.item_my_small_gv,null);
                viewHolder = new ViewHolder();
                viewHolder.tv_item_status_satified = (TextView) convertView.findViewById(R.id.tv_item_status_satified);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            MySeltctPayTime mySelectpayTime = list.get(position);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(120,80);
            layoutParams.setMargins(10, 5, 10, 5);//4个参数按顺序分别是左上右下搜索
            viewHolder.tv_item_status_satified.setLayoutParams(layoutParams);
            viewHolder.tv_item_status_satified.setGravity(Gravity.CENTER);
            if(mySelectpayTime.isClick()) {
                ImageUtils.setTextViewBg(viewHolder.tv_item_status_satified, cnt, R.drawable.shade_full_oval_small, mySelectpayTime.getSelestDate());
                viewHolder.tv_item_status_satified.setTextColor(cnt.getResources().getColor(R.color.white));
            }else{
                ImageUtils.setTextViewBg(viewHolder.tv_item_status_satified, cnt, R.drawable.shade_enpety_oval_small, mySelectpayTime.getSelestDate());
                viewHolder.tv_item_status_satified.setTextColor(cnt.getResources().getColor(R.color.theme_color));
            }
            return convertView;
        }
    }

    private class ViewHolder{
        private TextView tv_item_status_satified;
    }
}
